﻿using OnlineMealOrdering.Common;
using OnlineMealOrdering.Domain.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnlineMealOrdering.Domain
{
    public class FoodEntity : AuditableEntity
    {
        #region Property
        public string Name { get; set; }

        //是否为推荐菜品
        public bool Recommend { get; set; }

        public virtual ICollection<ImageEntity> Images { get; set; }

        public decimal CostPrice { get; set; }

        public decimal Price { get; set; }

        public virtual StoreOnlineEntity StoreOnline { get; set; }

        public virtual FoodCatalogEntity Catalog { get; set; }

        public virtual ICollection<FoodEntity> Gift { get; set; }

        #endregion

        #region Static

        public static FoodEntity Create(string name, decimal costPrice, decimal price, int storeid, int catalogId, bool recommed, List<FoodEntity> gift, List<ImageEntity> imgelist)
        {
            var store = DomainContext.Current.EntitySet<StoreOnlineEntity>().SingleOrDefault(a => a.Id == storeid);
            if (store == null)
                BusinessException.Create("找不到餐馆 ID:{0}", storeid);

            var catalog = DomainContext.Current.EntitySet<FoodCatalogEntity>().SingleOrDefault(a => a.Id == catalogId);
            if (catalog == null)
                BusinessException.Create("找不到目录 ID:{0}", catalogId);

            var entity = new FoodEntity();
            entity.Images = imgelist;
            entity.CostPrice = costPrice;
            entity.Price = price;
            entity.Name = name;
            entity.Catalog = catalog;
            entity.StoreOnline = store;
            entity.Gift = gift;
            entity.Recommend = recommed;
            return entity;
        }

        public void Update(string name, decimal costPrice, decimal price, int storeid, int catalogId, List<FoodEntity> gift, List<ImageEntity> imgelist)
        {
            var store = DomainContext.Current.EntitySet<StoreOnlineEntity>().SingleOrDefault(a => a.Id == storeid);
            if (store == null)
                BusinessException.Create("找不到餐馆 ID:{0}", storeid);

            var catalog = DomainContext.Current.EntitySet<FoodCatalogEntity>().SingleOrDefault(a => a.Id == catalogId);
            if (catalog == null)
                BusinessException.Create("找不到目录 ID:{0}", catalogId);

            Name = name;
            CostPrice = costPrice;
            Price = price;
            Catalog = catalog;
            StoreOnline = store;
            Gift = gift;
        }

        public void AddGift(int giftId)
        {
            var food = Gift.Single(a => a.Id == giftId);
            if (food == null)
                BusinessException.Create("找不到礼物 ID:{0}", giftId);

            var entity = DomainContext.Current.EntitySet<FoodCatalogEntity>().SingleOrDefault(a => a.Id == giftId);
            if (entity == null)
                BusinessException.Create("找不到餐品 ID:{0}", giftId);
            Gift.Add(food);
        }

        //TODO:Wating...
        public void UpdateGift(int giftId)
        {
            var food = Gift.Single(a => a.Id == giftId);
            if (food != null)
                Gift.Remove(food);

            Gift.Add(food);
            DomainContext.Current.SubmitChanges();
        }

        #endregion
    }
}
